<?php


namespace app\admin\controller\pointgoods;


use app\common\controller\Adminbase;
use think\Db;

class Prizegoods extends Adminbase
{
    protected $searchFields = 'name';

    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $_list = Db::name('prize_goods')
                ->order('is_d desc')
                ->order('sort desc')
//                ->order(array('sort' => 'desc', 'is_d' => 'asc'))
                ->where($where)
                ->where('is_del', '<>', 1)
                ->page($page, $limit)
                ->select();
            foreach ($_list as $k => $v) {
                //1优惠卷2生活用品3汽车配件
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($v['goods_type']==1){
                    $coupons=Db::name('coupons')->find($v['object_id']);
                    $_list[$k]['img'] =config('site.coupons_bg');
                    $_list[$k]['name'] = $coupons['name'];
                    $_list[$k]['goods_type'] = '优惠卷';
                }
                if ($v['goods_type']==2){
                    $_list[$k]['goods_type'] = '积分';
                }
                if ($v['goods_type']==3){
                    $_list[$k]['goods_type'] = '商品';
                }
                if ($v['is_d']==1){
                    $_list[$k]['probability'] = '保底商品';
                }
            }
            $total = Db::name('prize_goods')
                ->where($where)
                ->where('is_del', '<>', 1)
                ->count();
            $result = array("code" => 0, "count" => $total, "data" => $_list);
            return json($result);
        }
        return $this->fetch();
    }

    //增加
    public function add()
    {
        //1优惠卷 2积分 3商品
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($data['goods_type']==1){
                $coupons=Db::name('coupons')->find($data['object_id']);
                $insert_data=array(
                    'goods_type'=>$data['goods_type'],
                    'object_id'=>$data['object_id'],
                    'name'=>$coupons['name'],
                    'img'=>config('site.web_coupons_img_cj'),
                    'probability'=>$data['probability'],
                    'sort'=>$data['sort'],
                    'stock'=>99999999,
                    'create_time'=>time()
                );
            }
            if ($data['goods_type']==2){
                $insert_data=array(
                    'goods_type'=>$data['goods_type'],
                    'name'=>$data['point_num'].'积分',
                    'img'=>config('site.web_jf_img_cj'),
                    'point_num'=>$data['point_num'],
                    'sort'=>$data['sort'],
                    'stock'=>99999999,
                    'probability'=>$data['probability'],
                    'create_time'=>time()
                );
            }
            if ($data['goods_type']==3){
                $insert_data=array(
                    'goods_type'=>$data['goods_type'],
                    'name'=>$data['name'],
                    'img'=>$data['img'],
                    'stock'=>$data['stock'],
                    'probability'=>$data['probability'],
                    'sort'=>$data['sort'],
                    'is_limited'=>1,
                    'create_time'=>time()
                );
            }
            $stmt = Db::name('prize_goods')->insertGetId($insert_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }
        $goods_type = array(
            [
                'id' => 1,
                'name' => '优惠卷'
            ],
            [
                'id' => 2,
                'name' => '积分'
            ],
            [
                'id' => 3,
                'name' => '商品'
            ],
        );
        $coupons_list = Db::name('coupons')
            ->where([
                'is_del' => 0,
                'status' => 1,
                'cate_id'=>2
            ])->select();
        $this->assign([
            'goods_type' => $goods_type,
            'coupons_list'=>$coupons_list
        ]);
        return $this->fetch();
    }

    /**
     * 状态
     */
    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');

        if (Db::name('prize_goods')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }


    //删除
    public function del()
    {

        $ids = $this->request->param('ids/a', null);

        try {
            foreach ($ids as $id) {
                Db::name('prize_goods')->where('id', $id)->update([
                    'is_del' => 1
                ]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($data['goods_type']==1){
                $coupons=Db::name('coupons')->find($data['object_id']);
                $insert_data=array(
                    'id'=>$data['id'],
                    'goods_type'=>$data['goods_type'],
                    'object_id'=>$data['object_id'],
                    'name'=>$coupons['name'],
                    'img'=>config('site.web_coupons_img_cj'),
                    'probability'=>$data['probability'],
                    'sort'=>$data['sort'],
                    'stock'=>99999999,
                    'create_time'=>time()
                );
            }
            if ($data['goods_type']==2){
                $insert_data=array(
                    'id'=>$data['id'],
                    'goods_type'=>$data['goods_type'],
                    'name'=>$data['point_num'].'积分',
                    'img'=>config('site.web_jf_img_cj'),
                    'point_num'=>$data['point_num'],
                    'sort'=>$data['sort'],
                    'stock'=>99999999,
                    'probability'=>$data['probability'],
                    'create_time'=>time()
                );
            }
            if ($data['goods_type']==3){
                $insert_data=array(
                    'id'=>$data['id'],
                    'goods_type'=>$data['goods_type'],
                    'name'=>$data['name'],
                    'img'=>$data['img'],
                    'stock'=>$data['stock'],
                    'probability'=>$data['probability'],
                    'sort'=>$data['sort'],
                    'is_limited'=>1,
                    'create_time'=>time()
                );
            }

            $stmt = Db::name('prize_goods')->update($insert_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }
        $goods_type = array(
            [
                'id' => 1,
                'name' => '优惠卷'
            ],
            [
                'id' => 2,
                'name' => '积分'
            ],
            [
                'id' => 3,
                'name' => '商品'
            ],
        );
        $coupons_list = Db::name('coupons')
            ->where([
                'is_del' => 0,
                'status' => 1,
                'cate_id'=>2
            ])->select();
        $id = $this->request->param('id/d', 0);
        $data = Db::name('prize_goods')->find($id);
        $this->assign([
            'goods_type' => $goods_type,
            'data' => $data,
            'coupons_list'=>$coupons_list
        ]);
        return $this->fetch();
    }
}